package com.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Account;
import com.example.entity.Student;
import com.example.exception.CustomException;
import com.example.mapper.StudentMapper;
import com.example.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Override
    public void add(Student student) {
        //1.账号唯一验证
        Student dbStudent = studentMapper.selectByUsername(student.getUsername());
        if (ObjectUtil.isNotNull(dbStudent)) {
            throw new CustomException("用户名已存在");
        }
        //2.密码为空时，默认密码为123456
        if (ObjectUtil.isEmpty(student.getPassword())) {
            student.setPassword("123456");
        }
        //3.设置学分为0
        student.setScore(0);
        //4.名称为空时，设置默认名称为账号
        if (ObjectUtil.isEmpty(student.getName())) {
            student.setName(student.getUsername());
        }
        //5.默认角色为STUDENT
        student.setRole("STUDENT");
        //6.新增
        studentMapper.insert(student);
    }

    @Override
    public PageInfo<Student> selectPage(String name, Integer pageNum, Integer pageSize) {
        List<Student> studentList = null;
        PageHelper.startPage(pageNum, pageSize);
        if (ObjectUtil.isEmpty(name)) {
            studentList = studentMapper.selectAll();
        } else {
            studentList = studentMapper.selectByName(name);
        }
        return PageInfo.of(studentList);
    }

    @Override
    public void update(Student student) {
        //判断账号是否唯一
        //1.通过ID获取原student，然后判断当前的账号是否和原账号一致
        Student myStudent = studentMapper.selectById(student.getId());
        //2.如果原账号和新账号不一致，则判断账号是否已存在
        if (ObjectUtil.isNotNull(myStudent) && !myStudent.getUsername().equals(student.getUsername())) {
            //查询账号收否存在
            Student dbStudent = studentMapper.selectByUsername(student.getUsername());
            if (ObjectUtil.isNotNull(dbStudent)) {
                throw new CustomException("用户名已存在");
            }
        }
        studentMapper.updateById(student);
    }

    @Override
    public void deleteById(Integer id) {
        studentMapper.deleteById(id);
    }

    @Override
    public Student login(Account account) {
        //学生登录
        //1.通过账号查询学生，不存在则返回用户名不存在
        Student student = studentMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(student)){
            throw new CustomException("用户不存在");
        }
        //2.密码验证
        if (!account.getPassword().equals(student.getPassword())) {
            throw new CustomException("密码错误");
        }
        return student;
    }

    @Override
    public void register(Account account) {
        Student student = new Student();
        student.setUsername(account.getUsername());
        student.setPassword(account.getPassword());
        add(student);
    }

    @Override
    public Student selectById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public void updatePassword(Account account) {
        //1.获取原密码，判断原密码是否正确
        Student student = studentMapper.selectById(account.getId());
        if (ObjectUtil.isNull(student)) {
            throw new CustomException("用户不存在");
        }
        if (!account.getPassword().equals(student.getPassword())) {
            throw new CustomException("原密码错误");
        }
        student.setPassword(account.getNewPassword());
        studentMapper.updateById(student);
    }
}
